package com.tianqu.tms.www.car.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.entity.Car;

/**
 * @author moufeiyu 输入的车架号是否已经被登记过查询controller
 */
public class VinSearchController extends TmsController {
	private static final String RESPONSETEXT = "responseText";
	private Logger logger = Logger.getLogger(VinSearchController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			Car car = Car.getInstance();
			
			
			// 车辆ID
			String vin = (String)uc.getParameter("vin");
			
			boolean isExistFlg = car.isExistMarketCarByVin(vin);
			
			StringBuffer sb = new StringBuffer();
			sb.append(isExistFlg);
			
			controller.commit();
			String responseText = sb.toString();
			uc.setAttribute(RESPONSETEXT, responseText);
			
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");

	}
}
